use std::fmt::Debug;
use std::ops::{Add, AddAssign, Deref, Mul};
use anyhow::anyhow;

#[derive(Debug)]
pub struct Vector<T>
{
    data: Vec<T>,
}

// 点积
pub fn dot_product<T>(a: Vector<T>, b: Vector<T>) -> anyhow::Result<T>
    where T: Debug + Default + Copy + Add<Output=T> + AddAssign + Mul<Output=T>
{
    if a.len() != b.len() {
        return Err(anyhow!("a.len()!=b.len()"));
    }
    let mut res = Default::default();
    for i in 0..a.len() {
        res += a[i] * b[i];
    }
    Ok(res)
}

impl<T> Vector<T>
{
    pub fn new(data: Vec<T>) -> Vector<T> {
        Vector {
            data: data
        }
    }

    pub fn len(&self) -> usize {
        self.data.len()
    }


    pub fn iter(&self) -> std::slice::Iter<T> {
        self.data.iter()
    }
}


impl<T> Deref for Vector<T> {
    type Target = Vec<T>;
    fn deref(&self) -> &Self::Target {
        &self.data
    }
}